PHP OOP: Use an API with cURL
November 27, 2020
$meteo = new OpenWeater('b6907d289e10d714a6e88b30761fae22');
$meteo->getforecast('Montpellier,fr');
[ [ 'temp' => 5.03, 'description' => '...', 'date' => DateTime() ] ]
<?php
class OpenWeather {
const ICON_SIZE = '@2x'; // '', '@2x' or '@4x'
const UNITS = 'metric';
private $api_key;
public function __construct(string $var)
{
$this->api_key = $var;
}
public function getToday(array $coordinates): array
{
$array = [];
$data = $this->callAPI($coordinates);
if ($data !== null) {
$array = [
'temp' => $data["current"]["temp"] . '°C',
'description' => $data["current"]["weather"][0]["description"],
'icon' => 'http://openweathermap.org/img/wn/' . $data["current"]["weather"][0]["icon"] . self::ICON_SIZE . '.png',
'time' => $this->getTimeFormated($data["current"]["dt"], $data['timezone']),
];
}
return $array;
}
public function getLastHours(array $coordinates): array
{
$array = [];
$data = $this->callAPI($coordinates);
if ($data !== null) {
foreach ($data['hourly'] as $hour) {
$array[] = [
'temp' => $hour["temp"] . '°C',
'description' => ucfirst($hour["weather"][0]["description"]),
'icon' => 'http://openweathermap.org/img/wn/' . $hour["weather"][0]["icon"] . self::ICON_SIZE . '.png',
'time' => $this->getTimeFormated($hour["dt"], $data['timezone']),
];
}
}
return $array;
}
public function getTimeFormated($timestamp, $timezone): array
{
$date = new DateTime("@" . $timestamp);
$date->setTimeZone(new DateTimeZone($timezone));
return [
'all' => $date->format('M d H:i'),
'date' => $date->format('M d'),
'hour' => $date->format('H:i'),
];
}
private function callAPI(array $coordinates): ?array
{
$time = time();
$latitude = $coordinates[0];
$longitude = $coordinates[1];
$unit = self::UNITS;
$curl = curl_init("https://api.openweathermap.org/data/2.5/onecall/timemachine?lat={$latitude}&lon={$longitude}&dt=$time&units={$unit}&appid={$this->api_key}");
curl_setopt_array($curl, [
CURLOPT_CAINFO => dirname(__DIR__) . DIRECTORY_SEPARATOR . 'data' . DIRECTORY_SEPARATOR . 'certificates' . DIRECTORY_SEPARATOR . 'openweather.cer',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_TIMEOUT_MS => 1000
]);
$data = curl_exec($curl);
if ($data === false || curl_getinfo($curl, CURLINFO_HTTP_CODE) !== 200) {
return null;
}
curl_close($curl);
return json_decode($data, true);
}
}
<?php
$cities = [
'paris' => [48.85, 2.35],
'kerikeri' => [-35.22, 173.94],
'new-york' => [40.71, -74.00],
'hong-kong' => [22.39, 114.10],
];
$title = 'Weather forecast';
require_once 'includes/header.php';
require_once 'class/OpenWeather.php';
$weather = new OpenWeather('4f34eb4cec4aa5200aa8b415963e297c');
?>
<p class="lead mb-5">The list below has been made through the OpenWeather API. I links the API to this website thanks to the curl extension for PHP and following <a href="/lessons/20201127_php_Use%20an%20API%20with%20cURL/">this lesson</a>.</p>
<div class="row">
<?php
foreach ($cities as $name => $coordinates):
$current = $weather->getToday($coordinates);
if (!empty($current)): ?>
<div class="col-md-3 mb-5">
<div class="card">
<div class="card-body text-center">
<h4><?= implode('-', array_map('ucfirst', explode('-', $name))) ?></h4>
<div class="small text-muted"><?= $current['time']['all'] ?></div>
<img src="<?= $current['icon']; ?>"/>
<div><?= $current['description'] ?></div>
<div><b><?= $current['temp'] ?></b></div>
</div>
</div>
<div class="mt-3">
<ul class="list-unstyled">
<?php $lastHours = $weather->getLastHours($coordinates);
for ($i = count($lastHours) - 1; $i >= count($lastHours) - 10; $i--): ?>
<?php if (!empty($lastHours[$i])): ?>
<li class="small text-muted"><?= $lastHours[$i]['time']['hour'] . ' - <b>' . $lastHours[$i]['temp'] . '</b> - ' . $lastHours[$i]['description'] ?></li>
<?php endif; ?>
<?php
endfor; ?>
</ul>
</div>
</div>
<?php endif; ?>
<?php endforeach; ?>
</div>
<?php require 'includes/footer.php'; ?>
<?php
$curl = curl_init('https://samples.openweathermap.org/data/2.5/weather?q=Paris,fr&units=metric&APPID=b6907d289e10d714a6e88b30761fae22');
curl_setopt_array($curl, [
CURLOPT_CAINFO => __DIR__ . DIRECTORY_SEPARATOR . 'cert.cer',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_TIMEOUT_MS => 1000
]);
$data = curl_exec($curl);
if ($data === false) {
var_dump(curl_error($curl));
} else {
if(curl_getinfo($curl, CURLINFO_HTTP_CODE) === 200) {
$data = json_decode($data, true);
echo 'Temperature: ' . $data['main']['temp'] . ' °C';
}
}
curl_close($curl);
phpinfo();
command to get the info about your system. Look for a whole section called curl. If no section exists, then you have to install the extension via the php.ini file
extension_dir = "ext"
extension=curl
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_CAINFO, __DIR__ . DIRECTORY_SEPARATOR . 'cert.cer');
$data
variable with: curl_setopt_array( CURLOPT_RETURNTRANSFER => true);
json_decode($data, true);
var_dump($data['key']);
for example or any other PHP display function.
curl_setopt_array( CURLOPT_TIMEOUT_MS => 1000);
&APPID=paste_api_key_here